<template>
  <div>
    <el-dialog
      title="新增员工"
      :visible="showDialog"
      width="40%"
      @close="close"
    >
      <el-form
        ref="fm"
        :model="formData"
        :rules="rules"
        label-width="80px"
        :inline="false"
        size="normal"
      >
        <el-form-item label="姓名" prop="username">
          <el-input v-model="formData.username" />
        </el-form-item>
        <el-form-item label="手机" prop="moblie">
          <el-input
            v-model="formData.moblie"
            placeholder="请输入手机号"
            style="width: 50%"
          />
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-input
            v-model="formData.timeOfEntry"
            placeholder="请选择入职时间"
            style="width: 50%"
          />
        </el-form-item>
        <el-form-item label="聘用的形式" prop="formOfEmployment">
          <el-input
            v-model="formData.formOfEmployment"
            placeholder="请选择"
            style="width: 50%"
          />
        </el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input
            v-model="formData.workNumber"
            placeholder="请输入"
            style="width: 50%"
            @focus="showTree = tree"
          />
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <el-input
            v-model="formData.departmentName"
            style="width: 50%"
            placeholder="请选择部门"
            @focus="showTree = true"
          />
          <el-row
            v-show="showTree"
            style="
              position: relative;
              width: 280px;
              margin-top: 10px;
              broder: 1px solid #eee;
            "
          >
            <i
              style="position: absolute; top: 0; right: 0; z-index: 100"
              class="el-icon-delele"
              @click="showTree = false"
            />
            <el-tree
              :data="treeData"
              :props="{ label: 'name' }"
              @node-click="selectNode"
            />
          </el-row>
        </el-form-item>
        <el-form-item label="转正时间" prop="correctionTime">
          <el-date-picker
            v-model="formData.correctionTime"
            style="width: 50%"
            placeholder="请选择转正时间"
          />
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button @click="close">Cancel</el-button>
        <el-button type="primary" @click="add">OK</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import dataTypes from '@/api/constant/employees'
import { addEmployee } from '@/api/employees'
import { getDepartments } from '@/api/departments'
import { formatTreeData, parseTime } from '@/utils'
export default {
  props: {
    showDilog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      showTree: false,
      treeData: [],
      dataTypes,
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        timeOfEntry: '', // 入职时间
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          // pattern 传入正则
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created () {
    this.getTreedata()
  },
  methods: {
    add () {
      this.$refs.fm.validate(async (isOk) => {
        if (isOk) {
          this.formData.timeOfEntry = parseTime(this.formData.timeOfEntry, '{Y}-{M}-{D}')
          this.formData.correctionTime = parseTime(this.formData.correctionTime, '{Y}-{M}-{D}')
          await addEmployee(this.formData)
          this.$message.success('添加成功')
          this.close()
          this.parent.getList()
        }
      })
    },
    async getTreedata () {
      const { depts } = await getDepartments()
      this.treeData = formatTreeData(depts)
    },
    close () {
      this.$emit('close-dialog')
      this.$refs.fm.resetFields()
    }
  }

}
</script>

<style lang="scss" scoped>
</style>
